package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.bean.UserBean;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.contract.UpdateProfileContract;
import com.thinkfit.jy.dialog.UserPopupWindow;
import com.thinkfit.jy.presenter.UpdateProfilePresenter;
import com.thinkfit.jy.wheelview.DateTimeWheelView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 设置用户生日弹窗界面
 */
public class ChooseBirthdayFragment extends BaseFragment<UpdateProfileContract.Presenter> implements UpdateProfileContract.View {


    @NonNull
    @Override
    protected UpdateProfileContract.Presenter onCreatePresenter() {
        return new UpdateProfilePresenter(this);
    }


    @Override
    protected int getLayoutId() {
//        return DataManager.Companion.getInstance().getMainStyle() == 3 ?
//                R.layout.fragment_choose_birthday_xm :
        return R.layout.fragment_choose_birthday;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.tv_ok);
        //TODO 这边生日的日期会被设置成1970-01-01
        String birthday = DataManager.Companion.getInstance().getMyInfo().getBirthday();
        if (TextUtils.isEmpty(birthday)) {
            birthday = "1990-01-01";
        }
        DateTimeWheelView dateTimeWheelView = view.findViewById(R.id.dateTimeWheelView);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date startDate = calendar.getTime();
        Date endDate = Calendar.getInstance().getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(birthday);
        } catch (Exception e) {
            e.printStackTrace();
            date = new Date();
        }
        dateTimeWheelView.configShowUI(DateTimeWheelView.SHOW_YEAR_MONTH_DAY);
        dateTimeWheelView.setDateArea(startDate, endDate, true);
        if(date.getTime() <= startDate.getTime()){
            dateTimeWheelView.updateSelectedDate(startDate);
        }else {
            dateTimeWheelView.updateSelectedDate(date);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok:
                DateTimeWheelView dateTimeWheelView = getView().findViewById(R.id.dateTimeWheelView);
                UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
                String selectDate = dateTimeWheelView.getSelectDate();
                if (myInfo.getBirthday().equals(selectDate)) {
                    finish();
                    return;
                }
                UserPopupWindow.OnUserOlick onUserOlick = new UserPopupWindow.OnUserOlick() {
                    @Override
                    public void onClick() {
                        getPresenter().updateUser("birthday", selectDate);
                    }
                };
                UserPopupWindow userPopupWindow = new UserPopupWindow(getContext(), onUserOlick);
                userPopupWindow.show(getActivity());
                break;
        }
    }

    @Override
    public void updateUserSuccess(Object birthday) {
        if (getView() == null) {
            return;
        }
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        myInfo.setBirthday((String) birthday);
        DataManager.Companion.getInstance().saveMyInfo(myInfo);
        finish();
    }
}
